package com.et.u1.util;

import android.annotation.SuppressLint;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;

public class RepeatBtn {

    private final View view;

    public RepeatBtn(Button view) {
        this.view = view;
        if (view.getAnimation() != null) {
            view.getAnimation().cancel();
        }

        ScaleAnimation anim = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(300);
        view.startAnimation(anim);

        if (view.getAnimation() != null) {
            view.getAnimation().start();
        }

    }

    @SuppressLint("ClickableViewAccessibility")
    public void showRepeatBtn() {
        ScaleAnimation anim = new ScaleAnimation(0f, 0f, 0f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(320);
        view.startAnimation(anim);

        ScaleAnimation anim1 = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim1.setDuration(300);

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.startAnimation(anim1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        // 放大小时view，完全显示后开始呼吸效果
        ScaleAnimation enterAnim = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        enterAnim.setDuration(300); // 默认只执行一遍
        enterAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ScaleAnimation anim = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setDuration(250);
                anim.setRepeatMode(Animation.REVERSE); // 放大并缩
                anim.setRepeatCount(Animation.INFINITE); // 无限循环
                view.setAnimation(anim);
                view.startAnimation(view.getAnimation());
            }
        });
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.startAnimation(enterAnim);
            }
        });


        view.setOnTouchListener((v, event) -> {
            // 触摸时取消动画，并缩小，有按下的感觉
            if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
                view.setScaleX(0.9f);
                view.setScaleY(0.9f);
                if (view.getAnimation() != null) {
                    view.getAnimation().cancel();
                }
            } else { // 松手后，恢复大小，并继续呼吸效果
                view.setScaleX(1.0f);
                view.setScaleY(1.0f);
                if (view.getAnimation() != null) {
                    view.getAnimation().start();
                }
                view.startAnimation(enterAnim);
            }
            return false;
        });
    }

    /**
     * 点击动画
     */

    @SuppressLint("ClickableViewAccessibility")
    public void clickAnimation() {
        ScaleAnimation anim = new ScaleAnimation(0f, 0f, 0f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(320);
        view.startAnimation(anim);

        ScaleAnimation anim1 = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim1.setDuration(300);

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.startAnimation(anim1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        ScaleAnimation an = new ScaleAnimation(1.0f, 10f / 9f, 1.0f, 10f / 9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        an.setDuration(200);
        an.setFillAfter(true);
        an.setFillEnabled(true);
        view.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (view.getAnimation() != null) {
                    view.getAnimation().cancel();
                }
                view.clearAnimation();
                view.invalidate();
                view.setScaleX(0.9f);
                view.setScaleY(0.9f);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
                view.startAnimation(an);
            }
            return false;
        });
    }


}
